//
//  TransactionRecordViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/30.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "TransactionRecordViewController.h"
#import "TransactionRecordsModel.h"
#import "OrderDetailTransRecVC.h"
#import "TransactionRecordCell.h"

@interface TransactionRecordViewController () <UITableViewDataSource, UITableViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSArray *jsonArray;
@property (nonatomic, strong) NSMutableArray *contentArr;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *yearArray;
@property (nonatomic, strong) NSArray *monthArray;
@property (nonatomic, assign) NSInteger year;
@property (nonatomic, assign) NSInteger month;
@property (nonatomic, strong) NSString *startTime;
@property (nonatomic, strong) NSString *endTime;

@end

static NSString *trCellIdentifier = @"TransactionRecordCell";

@implementation TransactionRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initPickerView];
    [self titleView:@"交易记录"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"TransactionRecordCell" bundle:nil]
           forCellReuseIdentifier:trCellIdentifier];
    self.jsonArray = @[@{@"transrecImgRUL":@"",@"transrecOrderNum":@"128638432942455",@"transrecTitle":@"夏装刺绣短装宽松短袖棉麻t学女学生半袖亚麻衣服短款上衣女装",@"transrecPrice":@"-500.00",@"transrecCode":@"y3w90423095724545",@"transrecCreateTime":@"2015-07-01",@"transrectransNum":@"12380329473056",@"transrecPayType":@"账户余额"},
                       @{@"transrecImgRUL":@"",@"transrecOrderNum":@"128638432942455",@"transrecTitle":@"夏装刺绣短装宽松短袖棉麻t学女学生半袖亚麻衣服短款上衣女装",@"transrecPrice":@"-500.00",@"transrecCode":@"y3w90423095724545",@"transrecCreateTime":@"2015-07-02",@"transrectransNum":@"12380329473056",@"transrecPayType":@"账户余额"},
                       @{@"transrecImgRUL":@"",@"transrecOrderNum":@"128638432942455",@"transrecTitle":@"夏装刺绣短装宽松短袖棉麻t学女学生半袖亚麻衣服短款上衣女装",@"transrecPrice":@"-500.00",@"transrecCode":@"y3w90423095724545",@"transrecCreateTime":@"2015-07-03",@"transrectransNum":@"12380329473056",@"transrecPayType":@"账户余额"}];
    
    for (int i=0; i<self.jsonArray.count; i++) {
        TransactionRecordsModel *tranModel = [[TransactionRecordsModel alloc] init];
        [tranModel decodeDic:self.jsonArray[i]];
        [self.contentArr addObject:tranModel];
    }
//    [self requestData];
}

- (void)initPickerView {
    self.yearArray = @[@"2010",@"2011",@"2012",@"2013",@"2014",@"2015"];
    self.monthArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 150)];
    self.pickerView.showsSelectionIndicator=YES;
    [self.view addSubview:self.pickerView];
    self.pickerView.backgroundColor = [UIColor whiteColor];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.pickerView selectRow:5 inComponent:0 animated:YES];
    self.year = [[UIUtil date] year];
    self.month = [[UIUtil date] month];
    [self.pickerView selectRow:self.year inComponent:0 animated:YES];
    [self.pickerView selectRow:self.month - 1 inComponent:1 animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestData {
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken,@"timeStart":_startTime==nil?@"":_startTime,@"timeEnd":_endTime==nil?@"":_endTime};
    [RequestData priceQueryListParameters:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            self.jsonArray = jsonArray;
            [self.myTableView reloadData];
        }
    }];
}


//延迟实例化
//-(NSMutableArray *)contentArr
//{
//    if (_contentArr==nil) {
//        _contentArr = [[NSMutableArray alloc] init];
//    }
//    return _contentArr;
//}

#pragma mark - action methods
- (IBAction)chooseMonth:(id)sender {
    [UIView animateWithDuration:0.5 animations:^{
        self.pickerView.frame = CGRectMake(0, self.view.frame.size.height - self.pickerView.frame.size.height, SCREEN_WIDTH, self.pickerView.frame.size.height);
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([[UIUtil date] year] == self.year && [[UIUtil date] month] == self.month) {
        self.dateLabel.text = @"本月";
    } else {
        self.dateLabel.text = [NSString stringWithFormat:@"%ld年%ld月",(long)self.year,(long)self.month];
        _startTime = [NSString stringWithFormat:@"%ld-%ld",(long)self.year,(long)self.month];
        [self requestData];
    }
    [self canclePicker];
}

- (void) canclePicker{
    [UIView animateWithDuration:0.5 animations:^{
        self.pickerView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 150);
    }];
}

#pragma mark - Table view delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TransactionRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:trCellIdentifier];
    if (cell == nil) {
        cell = [[TransactionRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:trCellIdentifier];
    }
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderDetailTransRecVC *view = [[OrderDetailTransRecVC alloc] init];
    view.pathTransactionRecordsModel = self.contentArr[indexPath.row];
    [self canclePicker];
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.navigationController pushViewController:view animated:YES];
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return [self.yearArray count];
    }else {
        return [self.monthArray count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if(component == 0) {
        return [self.yearArray objectAtIndex:row];
    }else {
        return [self.monthArray objectAtIndex:row];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if(component == 0)
        return (SCREEN_WIDTH/2);
    return SCREEN_WIDTH/2;
}

#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.year = [[self.yearArray objectAtIndex:row] integerValue];
    }
    if (component == 1) {
        self.month = [[self.monthArray objectAtIndex:row] integerValue];
    }
    
}

@end
